package com.company.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;

import java.util.Arrays;

@Aspect
//告诉spring @Aspect注解的是一个切面类
public class LogAspect {

    //切入点表达式
    @Pointcut("execution(public int com.company.aop.MathCalculator.*(..))")
    public void pointCut() {
    }

    ;

    @Before("pointCut()")
    //joinPoint一定要出现在参数表第一位
    public void logStart(JoinPoint joinPoint) {
        Object[] arugs = joinPoint.getArgs();
        System.out.println("" + joinPoint.getSignature().getName() + "除法运行" + Arrays.asList(arugs));
    }

    //无论正常还是异常，方法结束就执行
    @After("pointCut()")
    public void logEnd() {
        System.out.println("除法结束");
    }

    @AfterReturning(value = "pointCut()", returning = "result")
    public void logReturn(Object result) {
        System.out.println("除法正常返回" + result);
    }

    @AfterThrowing("pointCut()")
    public void logError() {
        System.out.println("除法异常");
    }
}
